package com.outsourcing.bottle.ui;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.CareerEntry;
import com.outsourcing.bottle.domain.CareerInfo;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class SelectCareerActivity extends BasicActivity implements BasicUIEvent, Callback, OnClickListener {

	private final int init_career_list = 1;
	private final int error = 2;
	private Handler handler;
	
	private ImageView backView;
	private ExpandableListView listView;
	private List<CareerEntry> careerList;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.select_career);
		handler = new Handler(this);
		initWidget();
		UICore.eventTask(this, this, init_career_list, "init_career_list", null);
	}
	
	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		listView = (ExpandableListView) findViewById(R.id.select_career_expand_list);
		listView.setOnChildClickListener(new OnChildClickListener() {
			
			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				Intent intent = new Intent(SelectCareerActivity.this, SettingBasicActivity.class);
				intent.putExtra("career_class", careerList.get(groupPosition).getCareer_class());
				intent.putExtra("career_subclass", careerList.get(groupPosition).getSubcareers_list().get(childPosition).getCareer_subclass());
				intent.putExtra("career_classid", careerList.get(groupPosition).getCareer_classid());
				intent.putExtra("career_subclassid", careerList.get(groupPosition).getSubcareers_list().get(childPosition).getCareer_subclassid());
				setResult(getIntent().getIntExtra("resultCode", 0), intent);
				finish();
				return false;
			}
		});
	}
	
	private void initCareers() {
		StringBuffer url = new StringBuffer(UrlConfig.get_careers_list);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				Gson gson = new Gson();
				CareerInfo info = gson.fromJson(result, CareerInfo.class);
				if (info.getResults().getAuthok() == 0) {
					Message msg = Message.obtain(handler, error, info.getResults().getErrmsg());
					handler.sendMessage(msg);
				} else {
					careerList = info.getCareers_list();
					handler.sendEmptyMessage(init_career_list);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		}
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case init_career_list:
			initCareers();
			break;

		default:
			break;
		}
	}
	
	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case init_career_list:
			CareerAdapter adapter = new CareerAdapter();
			listView.setAdapter(adapter);
			break;
		case error:
			onToast((String) msg.obj);
			break;
		default:
			break;
		}
		return false;
	}
	
	public class CareerAdapter extends BaseExpandableListAdapter {

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			return careerList == null ? null : careerList.get(groupPosition)
					.getSubcareers_list().get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.simple_listitem2, null);
			}
			TextView name = (TextView) convertView.findViewById(R.id.simple_listitem_name);
			name.setText(careerList.get(groupPosition).getSubcareers_list().get(childPosition).getCareer_subclass());
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return careerList == null ? 0 : careerList.get(groupPosition).getSubcareers_list().size();
		}

		@Override
		public Object getGroup(int groupPosition) {
			return careerList == null ? null : careerList.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return careerList == null ? 0 : careerList.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.simple_listitem2, null);
			}
			TextView name = (TextView) convertView.findViewById(R.id.simple_listitem_name);
			name.setText(careerList.get(groupPosition).getCareer_class());
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			return true;
		}
		
	}
	
}
